// compareTimestamp.js
export function compareTimestamp(timestamp) {
    const currentTime = new Date().getTime();
    const timeDiff = currentTime - timestamp;

    if (timeDiff < 60000) {
        return '1分钟内';
    } else if (timeDiff < 3600000) {
        return `${Math.floor(timeDiff / 60000)}分钟`;
    } else if (timeDiff < 86400000) {
        return `${Math.floor(timeDiff / 3600000)}小时`;
    } else if (timeDiff < 2592000000) {
        return `${Math.floor(timeDiff / 86400000)}天`;
    } else if (timeDiff < 7776000000) {
        return `${Math.floor(timeDiff / 2592000000)}月`;
    } else {
        return null;
    }
}

// messageBox.js
import { ElMessageBox, ElNotification } from 'element-plus';

export function gotoHome() {
    ElMessageBox.alert('页面有误将返回首页', '提示', {
        confirmButtonText: '确定',
        callback: () => {
            window.location.href = '/#/';
        },
    });
}

export function gotoLogin() {
    ElMessageBox.alert('该功能需要登录，将返回登录页面', '提示', {
        confirmButtonText: '确定',
        callback: () => {
            window.location.href = '/#/login';
        },
    });
}

export function gotoRegister() {
    window.location.href = '/#/register';
}

/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
    ElNotification({
        title: '提示',
        message: content,
        type: 'info',
    });
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
    return ElMessageBox.confirm(content, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(() => true).catch(() => false);
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
    let result = '';
    for (const propName of Object.keys(params)) {
        const value = params[propName];
        const part = encodeURIComponent(propName) + '=';

        if (value !== null && value !== '' && typeof value !== 'undefined') {
            if (typeof value === 'object') {
                for (const key of Object.keys(value)) {
                    if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
                        const paramsStr = `${encodeURIComponent(propName)}[${encodeURIComponent(key)}]`;
                        const subPart = `${paramsStr}=`;
                        result += subPart + encodeURIComponent(value[key]) + '&';
                    }
                }
            } else {
                result += part + encodeURIComponent(value) + '&';
            }
        }
    }

    return result ? result.slice(0, -1) : result; // Remove the last '&' if it exists
}
